package com.cms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cms.common.MD5Util;
import com.cms.common.SiteUtil;
import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;
import com.cms.model.TrAdminUserLog;
import com.cms.service.AdminUserLogService;
import com.cms.service.ReportService;
import com.cms.service.AdminUserService;
import com.google.gson.Gson;

 

@Controller
public class LoginController extends BaseController{

	 @Autowired
     protected ReportService reportService;
	
	 @Autowired
	 protected AdminUserService userService;
	 
	 @Autowired
	 private AdminUserLogService userLogService;

	/***
	 *  进入首页登录界面
	 * @param req
	 * @param resp
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/index.do")
	public String toLogin(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	{
		return "login/login.html";
	}
	
	

	/***
	 * 登录的方法  登录成功跳转到主界面 
	 * @param req
	 * @param resp
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/login.do")
	public void login(HttpServletRequest req,HttpServletResponse resp)throws Exception
	{
		 String username =req.getParameter("username");
		 String password= req.getParameter("password");
		 String code =req.getParameter("checkCode");
		 String rand =StringUtils.obj2String(req.getSession().getAttribute("rand"));
		 HashMap<String, Object> map = new HashMap<String, Object>();
		 if(StringUtils.isBlank(code))
		 {
			 map.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			 map.put(CmsConstant.MSG, "验证码为空。"); 
		 }
		 else if(StringUtils.isBlank(rand))
		 {
			 map.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			 map.put(CmsConstant.MSG, "验证码已失效，请重新生成。"); 
		 }
		 else if(!StringUtils.equalsIgnoreCase(code, rand))
		 {
			 map.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			 map.put(CmsConstant.MSG, "验证码输入错误，请重新输入。"); 
		 }
		 else
		 {
		 TrAdminUser tr = new TrAdminUser(); 
		 tr.setUserLoginName(username);
		 tr = userService.login(tr); 
		 if(tr==null)
		 {
			 map.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			 map.put(CmsConstant.MSG, "用户名错误!请重新输入。"); 
		 }
		 else if(!StringUtils.equals(tr.getUserPassword(), MD5Util.MD5(password)))
		 {
			 map.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			 map.put(CmsConstant.MSG, "密码错误!请重新输入。"); 
		 }
		 else if(!StringUtils.equals(tr.getUserStatus(), CmsConstant.USER_STATUS_ENABLE))
		 {
			 map.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			 map.put(CmsConstant.MSG, "该账号已被停用，请联系系统管理员处理。"); 
		 }
		 else
		 {
			 map.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		     this.userService.updateLoginDate(tr);
		     TrAdminUserLog log =new TrAdminUserLog();
		     log.setLoginAddress(SiteUtil.getIpAddr(req)); 
		     log.setLoginName(tr.getUserLoginName()); 
		     log.setOperationSystem(SiteUtil.getOperationSystem(req));
		     log.setNavigator(SiteUtil.getNavigator(req));
		     log.setSessionId(req.getSession().getId());
		     userLogService.addAdminLog(log);
		     req.getSession().setAttribute(CmsConstant.CURRENT_USER, tr); 
		 }
		 }
		 this.renderText(resp, new Gson().toJson(map));
	}  
	
	@RequestMapping("/main.do")
	public String main(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	{
		 TrAdminUser tr=(TrAdminUser) req.getSession().getAttribute(CmsConstant.CURRENT_USER); 
		 if(tr==null)
		 {
			 return "redirect:/index.do";
		 }   
		 map.put("user", tr);
		 Map<String,Object>param = new HashMap<String, Object>();
		 param.put("id", tr.getId());
		 Map<String,Object>result= this.reportService.querySingleResult("queryReceiveMsg", param);
		 map.put("enableReceiveMsg", (result!=null&&!result.isEmpty()&&StringUtils.str2Int(result.get("result")+"")>0)?"Y":"N");
		 return "main/main.html";
	}
	
	
	@RequestMapping("/main/getFunctionList.do")
	public void getFunctionList(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	{
		 TrAdminUser tr=(TrAdminUser) req.getSession().getAttribute(CmsConstant.CURRENT_USER); 
		 Map<String,Object>result = new HashMap<String, Object>();
		 if(tr!=null)
		 {
			 HttpSession session= req.getSession();
			 Object fnList =session.getAttribute(CmsConstant.USER_FUNCTION_LIST);
			 if(fnList!=null)
			 {
				 result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
				 result.put(CmsConstant.USER_FUNCTION_LIST,fnList);
			 }
			 else
			 {
			 Map<String,Object>query=  new HashMap<String, Object>();
			 query.put("userId", tr.getId()); 
			 List<Map>funList = (List<Map>)this.reportService.getReportResult("getUserFunctionList", query);
			 if(funList==null||funList.isEmpty())
			 {
				 result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
				 result.put(CmsConstant.MSG, "该用户未授予权限，请授权后重新操作。");
			 }
			 else
			 {
				 result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
				 result.put(CmsConstant.USER_FUNCTION_LIST,funList);
				 session.setAttribute(CmsConstant.USER_FUNCTION_LIST, funList);
			 }
			 }
		 }
		 else
		 {
			 result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			 result.put(CmsConstant.MSG, "会话已经超时，请重新登陆。");
		 }
         this.renderText(resp, new Gson().toJson(result));
	}
	 
	@RequestMapping("/loginOut.do")
	public String loginOut(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	{ 
		 req.getSession().removeAttribute(CmsConstant.CURRENT_USER);
		 req.getSession().removeAttribute(CmsConstant.USER_FUNCTION_LIST);
		 req.getSession().invalidate();
		 return "redirect:/index.do";
	}
}
